package pandora.data.mysql;

import pandora.connection.ConnectionManager;
import pandora.data.*;
import pandora.dto.BrandDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCBrandDAO implements IBrandDAO {

	private Connection conn;
	
	public JDBCBrandDAO(){
		conn = ConnectionManager.getInstance().checkOut();
	}

    protected void finalize() {
        ConnectionManager.getInstance().checkIn(conn);
    }
    
	public void delete(Connection conn, String brandOID) {
		// TODO Auto-generated method stub
		
		String sql = "DELETE FROM Brand WHERE (OID = ?) ";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, brandOID);
			stmt.executeUpdate();
		} catch (SQLException e) {
			System.err.println("Message: " + e.getMessage());
			System.err.println("SQLState: " + e.getSQLState());
			System.err.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}
		}
	}

	public void insert(Connection conn, String oidb, String CIF) {
		// TODO Auto-generated method stub
		
	}

	
	public void insert(Connection conn, String oidb, BrandDTO brand) {
		// TODO Auto-generated method stub
		
	}

	
	public BrandDTO select(Connection conn, String oidb) {
		// TODO Auto-generated method stub
		return null;
	}

	//DCC
	public BrandDTO selectBrandByOID(String brandOID) {
		PreparedStatement stmt = null;
		ResultSet result = null;
		BrandDTO brand = null;
		
		String sql = "SELECT B.* FROM Brands B WHERE (B.OID = ?) ";
		
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, brandOID);
			result = stmt.executeQuery();
			if (result.next()) {
				brand = new BrandDTO();
				
				brand.setBrandName(result.getString("brandName"));
				brand.setBrandNIF(result.getString("brandNIF"));
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return brand;
	}
	//Fin DCC

}
